﻿using System;
using System.Linq;
using System.IO;
using Xpedite.WebAbstraction;
using System.Collections.Generic;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Resources
{
  internal interface IResourceCache
  {
    IResourceContainer Get(IResourceUrl url);
    IResourceContainer GetOrCreateWithNoDependency(IResourceUrl url, Func<IResourceContainer> factory);
    IResourceContainer GetOrCreateWithFileDependency(IResourceUrl url, Func<IResourceContainer> factory, String filename);
    IResourceContainer GetOrCreateWithResourceDependencies(IResourceUrl url, Func<IResourceContainer> factory, IEnumerable<IResourceContainer> resourceDependencies);
    void Remove(IResourceUrl url);
  }

  internal class ResourceCache : IResourceCache
  {
    private readonly Object SyncLock = new Object();
    private readonly IWebCache _webCache;

    public ResourceCache(IWebCache webCache)
    {
      Verify.NotNull(webCache, "webCache");

      _webCache = webCache;
    }

    public IResourceContainer Get(IResourceUrl url)
    {
      lock (SyncLock)
      {
        return _webCache.Get(url.VirtualPath);
      }
    }

    public IResourceContainer GetOrCreateWithNoDependency(IResourceUrl url, Func<IResourceContainer> factory)
    {
      return GetOrCreate(url.VirtualPath, factory, resource => _webCache.AddWithNoDependency(url.VirtualPath, resource));
    }

    public IResourceContainer GetOrCreateWithFileDependency(IResourceUrl url, Func<IResourceContainer> factory, String filename)
    {
      return GetOrCreate(url.VirtualPath, factory, resource =>
      {
        if (File.Exists(filename))
          _webCache.AddWithFileDependency(url.VirtualPath, resource, filename);
        else
          _webCache.AddWithNoDependency(url.VirtualPath, resource);
      });
    }

    public IResourceContainer GetOrCreateWithResourceDependencies(IResourceUrl url, Func<IResourceContainer> factory, IEnumerable<IResourceContainer> resourceDependencies)
    {
      return GetOrCreate(url.VirtualPath, factory, resource => _webCache.AddWithCacheDependencies(url.VirtualPath, resource, resourceDependencies.Select(item => item.ResourceUrl.VirtualPath).ToArray()));
    }

    private IResourceContainer GetOrCreate(String key, Func<IResourceContainer> factory, Action<IResourceContainer> cacher)
    {
      Verify.NotWhiteSpace(key, "key");
      Verify.NotNull(factory, "factory");

      lock (SyncLock)
      {
        IResourceContainer resourceContainer = _webCache.Get(key);
        if (resourceContainer != null)
          return resourceContainer;

        cacher.Invoke(resourceContainer = factory.Invoke());

        return resourceContainer;
      }
    }

    public void Remove(IResourceUrl url)
    {
      _webCache.Remove(url.VirtualPath);
    }
  }
}
